<template>
  <transition name="slide-up">
    <div class="setting-wrapper" v-show="menuVisible && settingVisible === 2">
      <div class="setting-progress">
        <div class="read-time-wrapper">
          <span class="reda-time-text">{{ getReadTimeText() }}</span>
          <span class="icon-forward"></span>
        </div>
        <div class="progress-wrapper">
          <div class="progress-icon-wrapper" @click="prevSection()">
            <span class="icon-back"></span>
          </div>
          <input
            class="progress"
            type="range"
            max="100"
            min="0"
            step="1"
            @change="onProgressChange($event.target.value)"
            @input="onProgressInput($event.target.value)"
            :value="progress"
            :disabled="!bookAvailable"
            ref="progress"
          />
          <div class="progress-icon-wrapper" @click="nextSection()">
            <span class="icon-forward"></span>
          </div>
        </div>
        <div class="text-wrapper">
          <span class="progress-section-text">{{ getSectionName }}</span>
          <span
            >({{ bookAvailable ? progress + "%" : $t("book.loading") }})</span
          >
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
import { ebookMixin } from '../../utils/mixin'

export default {
  mixins: [ebookMixin],
  methods: {
    // 点击进度条,百分比和书籍内容变化
    onProgressChange(progress) {
      this.setProgress(progress).then(() => {
        this.displayProgress()
        this.updateProgressBg()
      })
    },
    // 拖动进度条，百分比和书籍内容变化
    onProgressInput(progress) {
      this.setProgress(progress).then(() => {
        this.displayProgress()
        this.updateProgressBg()
      })
    },
    // 展示当前进度的页面
    displayProgress() {
      const cfi = this.currentBook.locations.cfiFromPercentage(this.progress / 100)
      // console.log(cfi)
      // this.currentBook.rendition.display(cfi).then(() => {
      //   this.refreshLocation()
      // })
      this.display(cfi)
    },
    // 进度条进度颜色变化
    updateProgressBg() {
      this.$refs.progress.style.backgroundSize = `${this.progress}% 100%`
    },
    // 返回上一章节 bookAvailable书籍是解析好的才有章节
    prevSection() {
      if (this.section > 0 && this.bookAvailable) {
        this.setSection(this.section - 1).then(() => {
          this.displaySection()
        })
      }
    },
    // 下一章节
    nextSection() {
      // spine 章节 从0开始
      // console.log(this.currentBook.spine)
      if (this.section < this.currentBook.spine.length - 1 && this.bookAvailable) {
        this.setSection(this.section + 1).then(() => {
          this.displaySection()
        })
      }
    },
    // 章节显示
    displaySection() {
      const sectionInfo = this.currentBook.section(this.section)
      // console.log(sectionInfo)
      if (sectionInfo && sectionInfo.href) {
        // this.currentBook.rendition.display(sectionInfo.href).then(() => {
        //   this.refreshLocation()
        // })
        this.display(sectionInfo.href)
      }
    }
    // // 刷新当前进度条位置 放入mixin中
    // refreshLocation() {
    //   const currentLocation = this.currentBook.rendition.currentLocation()
    //   // console.log(currentLocation)
    //   const startCfi = currentLocation.start.cfi
    //   const progress = this.currentBook.locations.percentageFromCfi(startCfi)
    //   // console.log(progress)
    //   this.setProgress(Math.floor(progress * 100))
    //   // 保存当前进度
    //   saveLocation(this.fileName, startCfi)
    // }
    // // 阅读时间显示 放入mixin
    // getReadTimeText() {
    //   return this.$t('book.haveRead').replace('$1', this.getReadTimeByMinute())
    // },
    // // 阅读时间转化为分钟 放入book.js中
    // getReadTimeByMinute() {
    //   const readTime = getReadTime(this.fileName)
    //   if (!readTime) {
    //     return 0
    //   } else {
    //     // ceil 向上去整
    //     return Math.ceil(readTime / 60)
    //   }
    // }
  },
  // 解决初始化进度条颜色问题
  // vue钩子函数实现
  updated() {
    this.updateProgressBg()
  }
}
</script>

<style lang="scss" scoped>
@import "../../assets/styles/global";

.setting-wrapper {
  position: absolute;
  bottom: px2rem(48);
  left: 0;
  z-index: 190;
  width: 100%;
  height: px2rem(90);
  background: white;
  box-shadow: 0 px2rem(-8) px2rem(8) rgba(0, 0, 0, 0.15);
  .setting-progress {
    position: relative;
    width: 100%;
    height: 100%;
    .read-time-wrapper {
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      height: px2rem(40);
      font-size: px2rem(12);
      @include center;
    }
    .progress-wrapper {
      width: 100%;
      height: 100%;
      @include center;
      padding: 0 px2rem(15);
      box-sizing: border-box;
      .progress-icon-wrapper {
        font-size: px2rem(20);
      }
      .progress {
        width: 100%;
        -webkit-appearance: none;
        height: px2rem(2);
        // background: -webkit-linear-gradient(#999, #999) no-repeat, #ddd;
        // background-size: 0 100%!important;
        margin: 0 px2rem(10);
        &:focus {
          outline: none;
        }
        &::-webkit-slider-thumb {
          -webkit-appearance: none;
          height: px2rem(20);
          width: px2rem(20);
          border-radius: 50%;
          background: white;
          box-shadow: 0 4px 4px 0 rgba(0, 0, 0, 0.15);
          border: px2rem(1) solid #ddd;
        }
      }
    }
    .text-wrapper {
      position: absolute;
      left: 0;
      bottom: px2rem(10);
      width: 100%;
      color: #333;
      font-size: px2rem(12);
      // text-align: center;
      padding: 0 px2rem(15);
      box-sizing: border-box;
      @include center;
      .progress-section-text {
        @include ellipsis;
      }
    }
  }
}
</style>
